package core.imust.com.imustcore.test.demo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import core.imust.com.imustcore.R;

/**
 * Created by lenovo on 2020/6/27.
 */

public class StudentAdapter extends BaseAdapter {
    private Context context;
    private List<Student> list;
    private int layoutId;

    public StudentAdapter(Context context,List<Student> list,int layoutId){
        this.context=context;
        this.list=list;
        this.layoutId=layoutId;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        StudentViewHolder holder=null;
        //view表示可以重用的行视图
        if (view==null){
            view=View.inflate(context,layoutId,null);
            holder=new StudentViewHolder();
            TextView textView= (TextView) view.findViewById(R.id.text);
            holder.text=textView;
            //还得把这个holder放在view里面
            view.setTag(holder);
        }else {//view!=null
            //重用它
            holder=(StudentViewHolder)view.getTag();
        }
        //写上一段显示逻辑
        Student stu = list.get(i);
        //格式 ：   姓名:xxx  年龄:19
        //TextView textView = (TextView) view.findViewById(R.id.text);
        String s = "姓名:"+stu.name+"  年龄:"+stu.age;
        //textView.setText(s);
        holder.text.setText(s);
        //参1，上下文  参2 布局  参3null;
        return view;
    }
    //定义一个ViewHolder
    static class StudentViewHolder{
        //View的内容
        public TextView text;
    }
}
